package com.jvxb.manage.livable.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    private static RedisTemplate redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisUtil.redisTemplate = redisTemplate;
    }

    public static RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }

    public static boolean removeKey(String key) {
        String[] split = key.split(",");
        if (split.length == 1) {
            return redisTemplate.delete(key);
        } else {
            Long delete = redisTemplate.opsForHash().delete(split[0], split[1]);
            return delete > 1;
        }
    }

    public static boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    public static Set getHashKey(String key) {
        return redisTemplate.boundHashOps(key).keys();
    }

    public static void putAll(String key, Map map) {
        redisTemplate.boundHashOps(key).putAll(map);
    }

    public static void put(Object maxKey, Object minKey, Object value) {
        redisTemplate.boundHashOps(maxKey).put(minKey, value);
    }


    public static Object getHash(Object maxKey, Object minKey) {
        return redisTemplate.opsForHash().get(maxKey, minKey);
    }

    public static Object getString(Object key) {
        return redisTemplate.opsForValue().get(key);
    }

    public static void setString(Object key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }


    public static void setString(Object key, Object value, long timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    public static void expire(String key, long second) {
        Long expire = redisTemplate.getExpire(key);
        if (expire == -1) {
            redisTemplate.expire(key, second, TimeUnit.SECONDS);
        }
    }

    public static void coverExpire(String key, long timeout) {
        redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
    }

    public static Long zcard(String key) {
        return redisTemplate.opsForZSet().zCard(key);
    }

    public static void zSet(String key, String value, Long score) {
        redisTemplate.opsForZSet().add(key, value, score);
    }

    public static Set zRange(String key, int begin, int end) {
        Set zSetValue = redisTemplate.opsForZSet().range(key, begin, end);
        return zSetValue;
    }

    public static Set zReverseRange(String key, int begin, int end) {
        Set zSetValue = redisTemplate.opsForZSet().reverseRange(key, begin, end);
        return zSetValue;
    }

    public static void zRemoveRange(String key, int begin, int end) {
        redisTemplate.opsForZSet().removeRange(key, begin, end);
    }

}
